from itsdangerous import BadSignature
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from meiduo_mall import settings


# 加密
def generate_access_token(openid):
    s = Serializer(secret_key=settings.SECRET_KEY, expires_in=600)
    token = s.dumps({'openid': openid})
    return token.decode()


# 解密
def check_access_token(openid):
    s = Serializer(secret_key=settings.SECRET_KEY, expires_in=600)
    try:
        data = s.loads(openid)
    except BadSignature:
        return None
    else:
        return data.get('openid')
